package com.dream.classcontrol;

import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.Window;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.dream.config.DevUIIDConfig;
import com.dream.fragment.ChuanglianFragment;
import com.dream.fragment.DengguanFragment;
import com.dream.fragment.DianyuanFragment;
import com.dream.fragment.KongtiaoFragment;
import com.dream.fragment.MubuFragment;
import com.dream.fragment.TouyingFragment;
import com.dream.sdkmanager.SDKManager;

public class MainActivity extends Activity implements View.OnClickListener {

    private FragmentManager fragmentManager;
    private FragmentTransaction fragmentTransaction;
    private KongtiaoFragment kongtiaoFragment;
    private DengguanFragment dengguanFragment;
    private MubuFragment mubuFragment;
    private TouyingFragment touyingFragment;
    private ChuanglianFragment chuanglianFragment;
    private DianyuanFragment dianyuanFragment;
    private FrameLayout frame;
    private LinearLayout llKongtiao;
    private ImageView imageKongtiao;
    private TextView tvKongtiao;
    private LinearLayout llDengguan;
    private ImageView imageDengguan;
    private TextView tvDengguan;
    private LinearLayout llMubu;
    private ImageView imageMubu;
    private TextView tvMubu;
    private LinearLayout llTouying;
    private ImageView imageTouying;
    private TextView tvTouying;
    private LinearLayout llChuanglian;
    private ImageView imageChuanglian;
    private TextView tvChuanglian;
    private LinearLayout llDianyuan;
    private ImageView imageDianyuan;
    private TextView tvDianyuan;
    private ImageView[] bottomImages;
    private TextView[] bottom_tv;
    private TextView tvWendu;
    private TextView tvShidu;
    private TextView tvHostId;
    private int count=0;
    private ImageView log;
    private SharedPreferences sharedPreferences;
    private SharedPreferences.Editor editor;
    private SDKManager sdkManager;
    private String roomWendu;
    private Message message;
    public String getRoomWendu() {
        return roomWendu;
    }

    private int[] imageSelected=new int[]{R.drawable.icon_kongtiao_selected,R.drawable.icon_dengguang_selected,R.drawable.icon_mubu_selected,R.drawable.icon_touying_selected,R.drawable.icon_chuanglian_selected,R.drawable.icon_dianyuan_selected,};
    private int[] imageUnSelect=new int[]{R.drawable.icon_kongtiao_unselect,R.drawable.icon_dengguang_unselect,R.drawable.icon_mubu_unselect,R.drawable.icon_touying_unselect,R.drawable.icon_chuanglian_unselect,R.drawable.icon_dianyuan_unselect,};

    public SDKManager getSdkManager() {
        return sdkManager;
    }

    private static final int MSG=0;
    private static final int WEIZONE_SET_VAL=1;
    private static final int SDK_STATE=2;

    private Handler handler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what){
                case MSG:
                    Toast.makeText(MainActivity.this,msg.obj.toString(),Toast.LENGTH_SHORT).show();
                    break;
                case WEIZONE_SET_VAL:
                    setWenShi((String[]) msg.obj);
                    break;
                case SDK_STATE:
                    tvHostId.setText((String)msg.obj);
                    break;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);
        initView();
        initSDK();
    }


    //初始化控件
    public void initView(){
        sharedPreferences=getSharedPreferences("appinfo",MODE_PRIVATE);
        editor=sharedPreferences.edit();
        tvHostId = (TextView) findViewById(R.id.hostId);
        log = (ImageView) findViewById(R.id.log);
        llKongtiao = (LinearLayout) findViewById(R.id.ll_kongtiao);
        imageKongtiao = (ImageView) findViewById(R.id.image_kongtiao);
        tvKongtiao = (TextView) findViewById(R.id.tv_kongtiao);
        llDengguan = (LinearLayout) findViewById(R.id.ll_dengguan);
        imageDengguan = (ImageView) findViewById(R.id.image_dengguan);
        tvDengguan = (TextView) findViewById(R.id.tv_dengguan);
        llMubu = (LinearLayout) findViewById(R.id.ll_mubu);
        imageMubu = (ImageView) findViewById(R.id.image_mubu);
        tvMubu = (TextView) findViewById(R.id.tv_mubu);
        llTouying = (LinearLayout) findViewById(R.id.ll_touying);
        imageTouying = (ImageView) findViewById(R.id.image_touying);
        tvTouying = (TextView) findViewById(R.id.tv_touying);
        llChuanglian = (LinearLayout) findViewById(R.id.ll_chuanglian);
        imageChuanglian = (ImageView) findViewById(R.id.image_chuanglian);
        tvChuanglian = (TextView) findViewById(R.id.tv_chuanglian);
        llDianyuan = (LinearLayout) findViewById(R.id.ll_dianyuan);
        imageDianyuan = (ImageView) findViewById(R.id.image_dianyuan);
        tvDianyuan = (TextView) findViewById(R.id.tv_dianyuan);
        tvWendu = (TextView) findViewById(R.id.tv_wendu);
        roomWendu=tvWendu.getText().toString();
        tvShidu = (TextView) findViewById(R.id.tv_shidu);
        bottomImages=new ImageView[]{imageKongtiao,imageDengguan,imageMubu,imageTouying,imageChuanglian,imageDianyuan};
        bottom_tv=new TextView[]{tvKongtiao,tvDengguan,tvMubu,tvTouying,tvChuanglian,tvDianyuan};
        //默认使用第0个Fragment
        fragmentManager = getFragmentManager();
        showFragment(0);
        setBottomBg(0);
    }

    public void initSDK(){
        sdkManager=new SDKManager(this);
        sdkManager.setSdkDataListener(new SDKManager.onGetData() {
            @Override
            public void onSearchFinish(String weiZoneIp) {

            }

            @Override
            public void getLandState(boolean isLand,String hostId) {
                System.out.println(isLand);
                message=new Message();
                message.what=SDK_STATE;
                if (isLand){
                    message.obj=hostId;
                    sdkManager.attentionVariableByUIID(DevUIIDConfig.wenshidu);
                }else{
                    message.obj="OFFLINE";
                }
                handler.sendMessage(message);
            }
            @Override
            public void getDevData(String UIID, String VALUE, String RFID, String INDEX, String DEVID, String HOST) {
                System.out.println("UIID："+UIID+" VALUE："+VALUE+" RFID："+RFID+" INDEX："+INDEX+" DEVID: "+DEVID+" HOST："+HOST);
                if (UIID.equals(DevUIIDConfig.wenshidu)){
                    message=new Message();
                    message.what=WEIZONE_SET_VAL;
                    message.obj=new String[]{UIID,VALUE,RFID,INDEX,DEVID,HOST};
                    handler.sendMessage(message);
                }
            }

            @Override
            public void getSetVarState(String s, String s1, String s2) {

            }

            @Override
            public void getSetKeyState(String s, String s1, String s2) {

            }
        });
        sdkManager.landingSDK();
        }

    public void setWenShi(String[] sdkInfoArr){
        if (sdkInfoArr[3].equals("1")){
            tvWendu.setText(sdkInfoArr[1]+"℃");
            roomWendu=sdkInfoArr[1]+"℃";
        }
        if (sdkInfoArr[3].equals("2")){
            tvShidu.setText(sdkInfoArr[1]+"%");
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.ll_kongtiao:
                showFragment(0);
                setBottomBg(0);
                break;
            case R.id.ll_dengguan:
                showFragment(1);
                setBottomBg(1);
                break;
            case R.id.ll_mubu:
                showFragment(2);
                setBottomBg(2);
                break;
            case R.id.ll_touying:
                showFragment(3);
                setBottomBg(3);
                break;
            case R.id.ll_chuanglian:
                showFragment(4);
                setBottomBg(4);
                break;
            case R.id.ll_dianyuan:
                showFragment(5);
                setBottomBg(5);
                break;
            //点击5次Log设置IP并保存在本地
            case R.id.log:
                count++;
                if (count==5){
                    count=0;
                    startActivity(new Intent(MainActivity.this,SettingActivity.class));
                    finish();
                }
                break;
        }
    }

    //显示fragment
    public void showFragment(int index){
        fragmentTransaction=fragmentManager.beginTransaction();
        hideFragment(fragmentTransaction);
        switch (index){
            case 0:
                if (kongtiaoFragment!=null){
                    fragmentTransaction.show(kongtiaoFragment);
                }else {
                    kongtiaoFragment=new KongtiaoFragment();
                    fragmentTransaction.add(R.id.frame,kongtiaoFragment);
                }
                break;
            case 1:
                if (dengguanFragment!=null){
                    fragmentTransaction.show(dengguanFragment);
                }else {
                    dengguanFragment=new DengguanFragment();
                    fragmentTransaction.add(R.id.frame,dengguanFragment);
                }
                break;
            case 2:
                if (mubuFragment!=null){
                    fragmentTransaction.show(mubuFragment);
                }else {
                    mubuFragment=new MubuFragment();
                    fragmentTransaction.add(R.id.frame,mubuFragment);
                }
                break;
            case 3:
                if (touyingFragment!=null){
                    fragmentTransaction.show(touyingFragment);
                }else {
                    touyingFragment=new TouyingFragment();
                    fragmentTransaction.add(R.id.frame,touyingFragment);
                }
                break;
            case 4:
                if (chuanglianFragment!=null){
                    fragmentTransaction.show(chuanglianFragment);
                }else {
                    chuanglianFragment=new ChuanglianFragment();
                    fragmentTransaction.add(R.id.frame,chuanglianFragment);
                }
                break;
            case 5:
                if (dianyuanFragment!=null){
                    fragmentTransaction.show(dianyuanFragment);
                }else {
                    dianyuanFragment=new DianyuanFragment();
                    fragmentTransaction.add(R.id.frame,dianyuanFragment);
                }
                break;
        }
        fragmentTransaction.commit();
    };

    //隐藏
    public void hideFragment(FragmentTransaction fragmentTransaction){
        if (kongtiaoFragment!=null){
            fragmentTransaction.hide(kongtiaoFragment);
        }
        if (dengguanFragment!=null){
            fragmentTransaction.hide(dengguanFragment);
        }
        if (mubuFragment!=null){
            fragmentTransaction.hide(mubuFragment);
        }
        if (touyingFragment!=null){
            fragmentTransaction.hide(touyingFragment);
        }
        if (chuanglianFragment!=null){
            fragmentTransaction.hide(chuanglianFragment);
        }
        if (dianyuanFragment!=null){
            fragmentTransaction.hide(dianyuanFragment);
        }
    }

    //设置底部菜单背景
    public void setBottomBg(int index){
        for (int i = 0; i < bottomImages.length; i++) {
            if (index==i){
                bottomImages[i].setImageResource(imageSelected[i]);
                bottom_tv[i].setTextColor(Color.parseColor("#1296DB"));
            }else {
                bottomImages[i].setImageResource(imageUnSelect[i]);
                bottom_tv[i].setTextColor(Color.parseColor("#929090"));
            }
        }
    }

    @Override
    protected void onDestroy() {
        sdkManager.exit();
        super.onDestroy();
    }

}
